<template>
  <div class="tree-item">
    <div v-for="item in treeData" :key="item.id">
      <div class="item-title" @click="itemNodeClick(item)">
        <span>{{ item.name }}</span>
        <span v-if="item.children && item.children.length">{{ isOpen(item.id)? '[-]' : '[+]' }}</span>
      </div>
      <div v-if="item.children && item.children.length" v-show="isOpen(item.id)" class="item-children">
        <my-tree :treeData="item.children" @node-click="$emit('node-click',$event)" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "myTree",
  data() {
    return {
      expandedKeys: [] // 当前列表需要展开的节点id组成的数组
    }
  },
  props: {
    treeData: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    itemNodeClick(item) {
      this.$emit("node-click", item)
      if (item.children && item.children.length) {
        let index = this.expandedKeys.indexOf(item.id)
        console.log(this.expandedKeys)
        if (index > -1) {
          // 如果当前节点id存在数组中，则删除
          this.expandedKeys.splice(index, 1)
        } else {
          // 如果当前节点id不存在数组中，则添加
          this.expandedKeys.push(item.id)
        }
      }
    },
    isOpen(id) {
      // 判断节点id在不在数组中，在则显示，不在则隐藏
      return this.expandedKeys.includes(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-item {
  cursor: pointer;
  .item-title {
    padding: 4px 8px;
    &:hover {
      background: #eee;
    }
  }
  .item-children {
    padding-left: 20px;
  }
}
</style>
